﻿using System;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using Zinnia.Lite.Entity;

namespace Zinnia.Lite.Map
{
    /// <summary>
    /// 数据库映射类约定接口
    /// </summary>
    public interface IZinniaEfBaseMap
    {
    }
    /// <summary>
    /// 映射类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ZinniaEfBaseMap<T> : EntityTypeConfiguration<T>, IZinniaEfBaseMap where T : ZinniaObject
    {
        public ZinniaEfBaseMap()
        {
            Init();
        }
        protected Type GenericType
        {
            get
            {
                return typeof(T);
            }
        }
        public virtual void Init()
        {
            ToTable(TableName(), SchemaName());
            HasKey(t => t.Id);
        }

        public virtual string TableName()
        {
            return GenericType.FullName.Substring(GenericType.FullName.LastIndexOf('.') + 1);
        }

        public virtual string SchemaName()
        {
            return "dbo";
        }
    }

    /// <summary>
    /// 数据库对象分割接口
    /// </summary>
    public interface IZinniaObjectInalienable
    {
        string GetTableName(Type type, IZinnia obj);
    }
}
